function Pagination($container,page_count) {
    this._$container = $container;
    this._count = page_count;
    this._current = 1;
    this.init();
}
Pagination.prototype.init = function() {
	this.render();
	this.eventCenter();
	this.selectPage(this._current);

};
Pagination.prototype.render = function() {
	var me = this;
	var template = __inline("./pagination.tmpl");
    var content = template({
    	current:me._current,
    	count:me._count
    });
    this._$container.append(content);
}
Pagination.prototype.redraw = function(type) {
	var me = this;
	if(type == 'pre') {
		$('.pagination .last').removeClass('disabled').bind('click',function() {
			if(event.preventDefault){
			    event.preventDefault();
			}else{
			    event.returnValue = false;
			}
			me.redraw('next');
		});
		var first_no = parseInt($('.pagination .page_no').eq(0).text());
		if(first_no <= 6) {
			$('.pagination .first').addClass('disabled').unbind('click');
			first_no = 1;
		} else {
			first_no = first_no-5;
		}
		for (var index in $('.pagination .page_no')) {
			$('.pagination .page_no').eq(index).text(parseInt(first_no) + parseInt(index));
		};
	} else if (type == 'next') {
		$('.pagination .first').removeClass('disabled').bind('click',function() {
			me.redraw('pre');
		});
		var first_no = parseInt($('.pagination .page_no').eq(4).text());
		if(first_no + 5 + 4 > me._count) {
			$('.pagination .last').addClass('disabled').unbind('click');
		}
		if(first_no + 5> me._count) {
			first_no = me._count-4;
		} else {
			first_no = first_no + 1;
		}
		for (var index in $('.pagination .page_no')) {
			$('.pagination .page_no').eq(index).text(parseInt(first_no) + parseInt(index));
		};
	}
	me.selectPage(me._current);
};
Pagination.prototype.selectPage = function(page_num) {
	$('.pagination li').removeClass('selected');
	var me = this;
	me._current = page_num;
	$('#goto_num').val(me._current);
	$(this).addClass('selected');
	for (var index in $('.pagination .page_no')) {
		if($('.pagination .page_no').eq(index).text() == me._current) {
			$('.pagination .page_no').eq(index).addClass('selected');
		}
	};
}
Pagination.prototype.dispose = function() {
	delete this._$container;
    delete this._count;
    delete this._current;
    $('#page_footer').remove();
}
Pagination.prototype.eventCenter = function() {
	var me = this;
	$('.pagination .page_no').bind('click', function () {
		if(event.preventDefault){
		    event.preventDefault();
		}else{
		    event.returnValue = false;
		}
		var goto_num = $(this).text(); 
		me.selectPage(goto_num);
		me._$container.trigger('changePage',{'goto_num' : goto_num});
	});
	$('.pagination .first,.last').addClass('disabled');
	if(me._count > 5) {
		$('.pagination .last').removeClass('disabled').bind('click' ,function() {
			if(event.preventDefault){
			    event.preventDefault();
			}else{
			    event.returnValue = false;
			}
			me.redraw('next');
		})
	}
	$('#page_footer .green_button').click(function () {
		var goto_num = $('#goto_num').val();
		if(me._count < goto_num) {
			alert('已超过最大页。');
		} else {
			me.selectPage(goto_num);
			me._$container.trigger('changePage',{'goto_num' : goto_num});
		}
	})
}
module.exports = Pagination;
